package br.com.classificadosonline.persistencia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import br.com.classificadosonline.nucleo.Status;


public class StatusDAO {

	private static final StatusDAO instance = null;

	public static StatusDAO getInstance()
	{
		if (instance == null)
		{
			instance = new StatusDAO();
		}
		return instance;
	}

	public static StatusDAO obterInstancia() {
		return (StatusDAO) Conexao.select("SELECT * FROM STATUS");
	}
	
	public static void cadastrarStatus(String descricao) {
		Conexao.update("INSERT INTO STATUS VALUES ('" + descricao + "')");
	}
	
	public static void modificarStatus(int codigo, String descricao) {
		Conexao.update("UPDATE STATUS SET DESCRICAO='" + descricao + "' WHERE CODIGO=" + codigo);
	}	
	
	public static Status buscarStatus(int codigo) {
		return buscar("SELECT * FROM STATUS WHERE CODIGO=" + codigo).get(0);
	}
	
	public static List<Status> buscar(String query) {
		ResultSet rs = null;
		List<Status> lista = new ArrayList<Status>();
		Status status = null;	
		Statement stmt;
		try {
			stmt = Conexao.conexao().createStatement();							
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				status = new Status(rs.getInt("CODIGO"), rs.getString("DESCRICAO"));
				lista.add(status);
			}
			stmt.close();
			Conexao.conexao().close();	
		} catch (SQLException e) {
		}
		return lista;
	}
	
	public static List<Status> listarStatus() {
		return buscar("SELECT * FROM STATUS");
	}
	
	public static void excluirStatus(int codigo) {
		Conexao.update("DELETE FROM STATUS WHERE CODIGO=" + codigo);
	}
}
